package app.myteamapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.List;
import java.util.Random;

public class MenuBebidas extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_bebidas);

        final MainApplication globalVariable = (MainApplication) getApplicationContext();

        RefreshCarrito();

        ImageButton cont1 = (ImageButton) findViewById(R.id.buttonAguaSinGas);
        cont1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Random myRandom = new Random(6);

                List<ItemCarrito> c = globalVariable.getCarrito();
                ItemCarrito item = new ItemCarrito();
                item.setId(myRandom.nextInt());
                item.setImageResource(R.drawable.agua);
                c.add(item);

                RefreshCarrito();
            }
        });

        ImageButton cont2 = (ImageButton) findViewById(R.id.buttonCocaCola);
        cont2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ItemCarrito> c = globalVariable.getCarrito();
                ItemCarrito item = new ItemCarrito();
                item.setImageResource(R.drawable.cocacola);
                c.add(item);

                RefreshCarrito();
            }
        });

        ImageButton cont3 = (ImageButton) findViewById(R.id.buttonFanta);
        cont3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ItemCarrito> c = globalVariable.getCarrito();
                ItemCarrito item = new ItemCarrito();
                item.setImageResource(R.drawable.fanta);
                c.add(item);

                RefreshCarrito();
            }
        });

        ImageButton cont4 = (ImageButton) findViewById(R.id.buttonSprite);
        cont4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ItemCarrito> c = globalVariable.getCarrito();
                ItemCarrito item = new ItemCarrito();
                item.setImageResource(R.drawable.sprite);
                c.add(item);

                RefreshCarrito();
            }
        });

        ImageButton cont5 = (ImageButton) findViewById(R.id.buttonCorona);
        cont5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ItemCarrito> c = globalVariable.getCarrito();
                ItemCarrito item = new ItemCarrito();
                item.setImageResource(R.drawable.corona);
                c.add(item);

                RefreshCarrito();
            }
        });

    }

    private View.OnClickListener mGreenBallOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("greeny", "Clicked on green ball->");

            final MainApplication globalVariable = (MainApplication) getApplicationContext();

            ItemCarrito item = (ItemCarrito) v.getTag();
            List<ItemCarrito> c = globalVariable.getCarrito();
            c.remove(item);

            LinearLayout ll = (LinearLayout) findViewById(R.id.lyCarrito);
            ll.removeView(v);
        }
    };


    private void RefreshCarrito() {
        final MainApplication globalVariable = (MainApplication) getApplicationContext();

        List<ItemCarrito> c = globalVariable.getCarrito();
        int count = c.size();

        LinearLayout ll = (LinearLayout) findViewById(R.id.lyCarrito);
        ll.removeAllViews();

        int j = 0;
        for (final ItemCarrito items : c) {
            ImageButton btnTag = new ImageButton(this);
            btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            btnTag.setImageResource(items.getImageResource());
            btnTag.setId(j++);
            btnTag.setOnClickListener(mGreenBallOnClickListener);
            btnTag.setTag(items);
            ll.addView(btnTag);
        }
    }
}
